package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.VO.SnackVO;
import com.student.VO.productPageVO;
import com.student.dto.ResponseDto;
import com.student.mapper.SnackGoodsMapper;
import com.student.pojo.Carousel;
import com.student.pojo.SnackGoods;
import com.student.pojo.SnackGoodsCate;
import com.student.service.SnackGoodsService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: makeups
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2024-12-19 22:59
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class SnackGoodsServiceImpl implements SnackGoodsService {

    @Autowired
    private SnackGoodsMapper snackGoodsMapper;

    @Override
    public ResponseDto getAllSnack(SnackVO product) {

        PageHelper.startPage(product.getCurrentPage(), product.getPageSize());
        List<SnackGoods> phAdminDO = snackGoodsMapper.getAllMakeup(product);

        PageInfo<SnackGoods> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto getSnackType() {
        List<SnackGoodsCate> productTypes = snackGoodsMapper.getSnackType();
        if (productTypes.size() > 0) {
            return ResponseDto.success(productTypes);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto updateGoods(SnackGoods updateProduct) {

        if (updateProduct.getImages()==null){
            Integer res =snackGoodsMapper.updateGoods(updateProduct);
            if (res > 0) {
                return ResponseDto.success(null);
            }
        }else {
            Integer res =snackGoodsMapper.updateGoodss(updateProduct);
            if (res > 0) {
                return ResponseDto.success(null);
            }
        }

        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteProduct(Integer id) {
        Integer res = snackGoodsMapper.deleteProduct(id);
        if (res > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }



    /**
     * 这边是做轮播图的管理
     * @param productPageVO
     * @return
     */
    @Override
    public ResponseDto getCarousel(Carousel productPageVO) {

        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(productPageVO.getCurrentPage(), productPageVO.getPageSize());
        List<Carousel> phAdminDO = snackGoodsMapper.getCarousel();

        PageInfo<Carousel> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto deleteCarouse(Integer id) {
        Integer res=snackGoodsMapper.deleteCarouse(id);
        if (res > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addCarousel(String productImage) {
        Integer res =snackGoodsMapper.addCarousel(productImage);
        if (res > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addGoods(SnackGoods product) {
        Integer res=snackGoodsMapper.addGoods(product);
        if (res > 0) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getCarousels() {
        List<Carousel> phAdminDO = snackGoodsMapper.getCarousel();
        if (phAdminDO.size() > 0) {
            return ResponseDto.success(phAdminDO);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getAllproduct(productPageVO productPage) {
//        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(productPage.getCurrentPage(), productPage.getPageSize());
        List<SnackGoods> phAdminDO = snackGoodsMapper.getAllproductTypes(productPage);

        PageInfo<SnackGoods> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto getAllproductSales(productPageVO productPage) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(productPage.getCurrentPage(), productPage.getPageSize(),"month_snack DESC");
        List<SnackGoods> phAdminDO = snackGoodsMapper.getAllproduct();

        PageInfo<SnackGoods> cyCardPageInfo = new PageInfo<>(phAdminDO);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto SingleSnack(Integer id) {
        SnackGoods snackGoods=snackGoodsMapper.SingleSnack(id);
        if (snackGoods!=null){
            return ResponseDto.success(snackGoods);
        }
        return ResponseDto.error(null);
    }
}